package com.hanter.demo.raddemo

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.collection.SparseArrayCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.hanter.android.radui.mvp.MvpTitleBarActivity
import com.hanter.android.radwidget.LazyFragment
import com.ogaclejapan.smarttablayout.SmartTabLayout
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems
import java.lang.ref.WeakReference


class LazyFragmentActivity :
    MvpTitleBarActivity<LazyFragmentContract.View, LazyFragmentContract.Presenter>(),
    LazyFragmentContract.View {

    override fun getLayout(): Int = R.layout.activity_lazy_fragment

    override fun createPresenter() = LazyFragmentPresenter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val adapter = MyFragmentPagerItemAdapter(
            supportFragmentManager, FragmentPagerItems.with(this)
                .add("titleA", PageFragment::class.java)
                .add("titleB", PageFragment::class.java)
                .add("titleC", PageFragment::class.java)
                .add("titleD", PageFragment::class.java)
                .add("titleE", PageFragment::class.java)
                .add("titleF", PageFragment::class.java)
                .create()
        )

        val viewPager = findViewById<View>(R.id.viewpager) as ViewPager
        viewPager.adapter = adapter

        val viewPagerTab = findViewById<View>(R.id.viewpagertab) as SmartTabLayout
        viewPagerTab.setViewPager(viewPager)
    }

    class MyFragmentPagerItemAdapter(fm: FragmentManager, private val pages: FragmentPagerItems) :
        FragmentPagerAdapter(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

        private val holder: SparseArrayCompat<WeakReference<Fragment>>

        init {
            this.holder = SparseArrayCompat(pages.size)
        }

        override fun getCount(): Int {
            return pages.size
        }

        override fun getItem(position: Int): Fragment {
            return getPagerItem(position).instantiate(pages.context, position)
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val item = super.instantiateItem(container, position)
            if (item is Fragment) {
                holder.put(position, WeakReference(item))
            }
            return item
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            holder.remove(position)
            super.destroyItem(container, position, `object`)
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return getPagerItem(position).title
        }

        override fun getPageWidth(position: Int): Float {
            return super.getPageWidth(position)
        }

        fun getPage(position: Int): Fragment? {
            val weakRefItem = holder.get(position)
            return weakRefItem?.get()
        }

        protected fun getPagerItem(position: Int): FragmentPagerItem {
            return pages[position]
        }

    }


    class PageFragment: LazyFragment() {

        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            Log.d(LazyFragment.TAG, "fragment: " + getTag() + " onCreateView")

            return inflater.inflate(R.layout.fragment_one, container, false)

        }

        override fun onFirstUserVisible() {
            Log.d(LazyFragment.TAG, "fragment: " + getTag() + " onFirstUserVisible")
        }

        override fun onUserVisible() {
            Log.d(LazyFragment.TAG, "fragment: " + getTag() +" onUserVisible")
        }

        override fun onFirstUserInvisible() {
            Log.d(LazyFragment.TAG, "fragment: " + getTag() +" onFirstUserInvisible")
        }

        override fun onUserInvisible() {
            Log.d(LazyFragment.TAG, "fragment: " + getTag() +" onUserInvisible")
        }

    }

}
